package fzrj.qkl.demo.controller;

import fzrj.qkl.demo.model.CommonResponse;
import fzrj.qkl.demo.service.backend.CarrierService;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/carrier")
public class CarrierController {

    @Autowired
    private CarrierService carrierService;

    // 承运商注册
    @PostMapping("/register")
    public CommonResponse registerCarrier(@RequestParam String name, 
                                        @RequestParam String info, 
                                        @RequestParam String password) {
        boolean res = carrierService.addCarrier(name, info, password);
        if (res) {
            return CommonResponse.ok(res);
        }
        return CommonResponse.fail("2001", new Exception("承运商注册失败"));
    }

    // 承运商登录验证
    @PostMapping("/login")
    public CommonResponse loginCarrier(@RequestParam int id, 
                                      @RequestParam String password) throws ContractException {
        boolean res = carrierService.checkCarrier(id, password);
        if (res) {
            return CommonResponse.ok(res);
        }
        return CommonResponse.fail("2002", new Exception("承运商登录失败"));
    }

}